import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/controllers/main/exit_dialog_view.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'main_logic.dart';

class MainPage extends StatelessWidget {
  MainPage({super.key});

  final logic = Get.find<MainLogic>();
  final state = Get.find<MainLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Platform.isAndroid ? PopScope(
        canPop: false,
        onPopInvoked: (didPop) => (onDidPop(context, didPop)),
        child: _buildScaffold(context)
    ) : _buildScaffold(context);
  }

  Scaffold _buildScaffold(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Obx(() => BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          selectedFontSize: 31.sp,
          unselectedFontSize: 31.sp,
          selectedItemColor: ColorConst.activeColor,
          unselectedItemColor: ColorConst.secondaryFontColor,
          onTap: (page) => logic.goToTab(page),
          currentIndex: logic.selectPage.value,
          items: [
            _bottomNavigationBarItem({"icon": "assets/images/home/home.png", "activeIcon": "assets/images/home/home_active.png", "label":LocaleKeys.home.ts}),
            _bottomNavigationBarItem({"icon": "assets/images/sports/sports.png", "activeIcon": "assets/images/sports/sports_active.png", "label":LocaleKeys.sports.ts}),
            _bottomNavigationBarItem({"icon": "assets/images/device/device.png", "activeIcon": "assets/images/device/device_active.png", "label":LocaleKeys.device.ts}),
            _bottomNavigationBarItem({"icon": "assets/images/space/space.png", "activeIcon": "assets/images/space/space_active.png", "label":LocaleKeys.space.ts}),
          ])),
      body: PageView.builder(
          itemCount: logic.pages.length,
          scrollDirection: Axis.horizontal,
          controller: logic.pageController,
          physics: const NeverScrollableScrollPhysics(),
          onPageChanged: (index) {
            logic.selectPage.value = index;
          },
          itemBuilder: (context, position) {
            return logic.pages[position];
            // return FadeTransition(opacity:logic.opacityAnim, child: logic.pages[position]);
          }),
    );
  }

  BottomNavigationBarItem _bottomNavigationBarItem(item) {
    return BottomNavigationBarItem(
      icon: Image.asset(item["icon"], width: 91.w, height: 91.w),
      activeIcon: Image.asset(item["activeIcon"], width: 91.w, height: 91.w),
      label: item["label"],
    );
  }

  showExitDialog(BuildContext context) {
    showDialog(context: context,
        barrierDismissible: false,
        builder: (_){
      return ExitDialog(callback: (value) async {
        if(value==0) {
          await pop();
        } else {
          UTEPedDevice.instance.getUteConfig().exitApp();
          exit(0);
        }
      },);
    });
  }

  onDidPop(BuildContext context, bool didPop) {
    if (Platform.isAndroid) {
      Get.bottomSheet(Container(
        decoration: const BoxDecoration(color: Colors.white),
        child: Wrap(
          children: [
            ListTile(
              title: Center(child: Text(LocaleKeys.exit_app.ts)),
              onTap: () {
                showExitDialog(context);
                // UTEPedDevice.instance.getUteConfig().exitApp();
                // exit(0);
              },
            ),
            ListTile(
              title: Center(
                child: Text(
                  LocaleKeys.app_run_background.ts,
                  style: TextStyle(color: ColorConst.accentColor),
                ),
              ),
              onTap: () async {
                await pop();
              },
            )
          ],
        ),
      ));
    }
  }

  static Future<void> pop() async {
    await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  }
}
